package com.sunshuo.springsecuritydemo.controller;

import com.sunshuo.springsecuritydemo.pojo.Role;
import com.sunshuo.springsecuritydemo.service.RoleService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName RoleController
 * @Description
 * @Author sunshuo
 * @Date 2021/11/7 19:12
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @RequestMapping("/page")
    @ResponseBody
    public String page() {
        JSONObject result = new JSONObject();
        result.put("url", "/role/role.jsp");
        return result.toString();
    }

    @RequestMapping("/query")
    @ResponseBody
    public String roleList() {
        List<Role> roles = roleService.getRoles();
        JSONObject result = new JSONObject();
        result.put("code", 0);
        result.put("msg", "");
        result.put("count", roles.size());
        JSONArray data = new JSONArray();
        roles.forEach(role -> {
            JSONObject roleJSON = new JSONObject();
            roleJSON.put("id", role.getId());
            roleJSON.put("name", role.getName());
            roleJSON.put("remarks", role.getRemarks());
            data.put(roleJSON);
        });
        result.put("data", data);
        return result.toString();
    }

}
